<?php

class CouponsController extends GxController {

    public function filters() {
        return array('rights');
    }

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id, 'Coupons'),
        ));
    }

    public function actionCreate() {
        $model = new Coupons;
        $ClassCourseCategoryUsersRestrictionCoupon = new ClassCourseCategoryUsersRestrictionCoupon;

        if (isset($_POST['Coupons'])) {
            /* echo "<pre>";
              print_r($_POST);
              die; */
            $model->setAttributes($_POST['Coupons']);
            $model->setAttribute('created', SkillConfig::CreateDateTime());
            $class_course_count = 0;
            if ($model->save()) {
                if (isset($_POST['ClassCourseCategoryUsersRestrictionCoupon'])) {
                    if (isset($_POST['ClassCourseCategoryUsersRestrictionCoupon']['classes_id']) && !isset($_POST['ClassCourseCategoryUsersRestrictionCoupon']['courses_id'])) {
                        $_POST['ClassCourseCategoryUsersRestrictionCoupon']['classes_id'] = rtrim($_POST['ClassCourseCategoryUsersRestrictionCoupon']['classes_id'], ",");
                        $classcount = explode(",", $_POST['ClassCourseCategoryUsersRestrictionCoupon']['classes_id']);
                        $classcountTotal = count($classcount);
                    } elseif (!isset($_POST['ClassCourseCategoryUsersRestrictionCoupon']['classes_id']) && isset($_POST['ClassCourseCategoryUsersRestrictionCoupon']['courses_id'])) {
                        $_POST['ClassCourseCategoryUsersRestrictionCoupon']['courses_id'] = rtrim($_POST['ClassCourseCategoryUsersRestrictionCoupon']['courses_id'], ",");
                        $coursecount = explode(",", $_POST['ClassCourseCategoryUsersRestrictionCoupon']['courses_id']);
                        $coursecountTotal = count($coursecount);
                    } elseif (isset($_POST['ClassCourseCategoryUsersRestrictionCoupon']['classes_id']) && isset($_POST['ClassCourseCategoryUsersRestrictionCoupon']['courses_id'])) {
                        $_POST['ClassCourseCategoryUsersRestrictionCoupon']['classes_id'] = rtrim($_POST['ClassCourseCategoryUsersRestrictionCoupon']['classes_id'], ",");
                        $_POST['ClassCourseCategoryUsersRestrictionCoupon']['courses_id'] = rtrim($_POST['ClassCourseCategoryUsersRestrictionCoupon']['courses_id'], ",");
                        $classcount = explode(",", $_POST['ClassCourseCategoryUsersRestrictionCoupon']['classes_id']);
                        $classcountTotal = count($classcount);
                        $coursecount = explode(",", $_POST['ClassCourseCategoryUsersRestrictionCoupon']['courses_id']);
                        $coursecountTotal = count($coursecount);
                    }
                    if ($classcountTotal >= $coursecountTotal) {
                        $class_course_count = $classcountTotal;
                    } else {
                        $class_course_count = $coursecountTotal;
                    }
                    for ($i = 0; $i < $class_course_count - 1; $i++) {
                        $ClassCourseCategoryUsersRestrictionCoupon = new ClassCourseCategoryUsersRestrictionCoupon;
                        $ClassCourseCategoryUsersRestrictionCoupon->setAttribute('coupons_id', $model->id);
                        if (isset($classcount[$i])) {
                            if ($classcount[$i] == "All") {
                                $ClassCourseCategoryUsersRestrictionCoupon->setAttribute('classes_id', 0);
                            }
                            $classValue = Classes::model()->findByAttributes(array('class_titile' => trim($classcount[$i])));
                            if (isset($classValue->id) && $classValue->id <> "") {
                                $ClassCourseCategoryUsersRestrictionCoupon->setAttribute('classes_id', $classValue->id);
                            }
                        }if (isset($coursecount[$i])) {
                            if ($coursecount[$i] == "All") {
                                $ClassCourseCategoryUsersRestrictionCoupon->setAttribute('courses_id', 0);
                            }
                            $courseValue = Courses::model()->findByAttributes(array('course_titile' => trim($coursecount[$i])));
                            if (isset($courseValue->id) && $courseValue->id <> "") {
                                $ClassCourseCategoryUsersRestrictionCoupon->setAttribute('courses_id', $courseValue->id);
                            }
                        }
                        $ClassCourseCategoryUsersRestrictionCoupon->save();
                    }
                }
                if (Yii::app()->getRequest()->getIsAjaxRequest())
                    Yii::app()->end();
                else
                    $this->redirect(array('admin'));
            }
        }

        $this->render('create', array('model' => $model, 'ClassCourseCategoryUsersRestrictionCoupon' => $ClassCourseCategoryUsersRestrictionCoupon));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id, 'Coupons');
        $updateclass = '';
        $updatecourse = '';
        $ClassCourseCategoryUsersRestrictionCoupon = new ClassCourseCategoryUsersRestrictionCoupon;

        if (isset($_POST['Coupons'])) {
            Coupons::deleteClassCourseCoupon($id);
            $model->setAttributes($_POST['Coupons']);
            if ($model->save()) {
                if (!$model->isNewRecord) {
                    $model->deleteClass_course_category_users_restriction_coupon($model->id);
                }
                if (isset($_POST['ClassCourseCategoryUsersRestrictionCoupon'])) {
                    if (isset($_POST['ClassCourseCategoryUsersRestrictionCoupon']['classes_id']) && !isset($_POST['ClassCourseCategoryUsersRestrictionCoupon']['courses_id'])) {
                        $_POST['ClassCourseCategoryUsersRestrictionCoupon']['classes_id'] = rtrim($_POST['ClassCourseCategoryUsersRestrictionCoupon']['classes_id'], ",");
                        $classcount = explode(",", $_POST['ClassCourseCategoryUsersRestrictionCoupon']['classes_id']);
                        $classcountTotal = count($classcount);
                    } elseif (!isset($_POST['ClassCourseCategoryUsersRestrictionCoupon']['classes_id']) && isset($_POST['ClassCourseCategoryUsersRestrictionCoupon']['courses_id'])) {
                        $_POST['ClassCourseCategoryUsersRestrictionCoupon']['courses_id'] = rtrim($_POST['ClassCourseCategoryUsersRestrictionCoupon']['courses_id'], ",");
                        $coursecount = explode(",", $_POST['ClassCourseCategoryUsersRestrictionCoupon']['courses_id']);
                        $coursecountTotal = count($coursecount);
                    } elseif (isset($_POST['ClassCourseCategoryUsersRestrictionCoupon']['classes_id']) && isset($_POST['ClassCourseCategoryUsersRestrictionCoupon']['courses_id'])) {
                        $_POST['ClassCourseCategoryUsersRestrictionCoupon']['classes_id'] = rtrim($_POST['ClassCourseCategoryUsersRestrictionCoupon']['classes_id'], ",");
                        $_POST['ClassCourseCategoryUsersRestrictionCoupon']['courses_id'] = rtrim($_POST['ClassCourseCategoryUsersRestrictionCoupon']['courses_id'], ",");
                        $classcount = explode(",", $_POST['ClassCourseCategoryUsersRestrictionCoupon']['classes_id']);
                        $classcountTotal = count($classcount);
                        $coursecount = explode(",", $_POST['ClassCourseCategoryUsersRestrictionCoupon']['courses_id']);
                        $coursecountTotal = count($coursecount);
                    }
                    if ($classcountTotal >= $coursecountTotal) {
                        $class_course_count = $classcountTotal;
                    } else {
                        $class_course_count = $coursecountTotal;
                    }

                    for ($i = 0; $i < $class_course_count - 1; $i++) {
                        $ClassCourseCategoryUsersRestrictionCoupon = new ClassCourseCategoryUsersRestrictionCoupon;
                        $ClassCourseCategoryUsersRestrictionCoupon->setAttribute('coupons_id', $model->id);
                        if (isset($classcount[$i])) {
                            if ($classcount[$i] == "All") {
                                $ClassCourseCategoryUsersRestrictionCoupon->setAttribute('classes_id', 0);
                            }

                            $classValue = Classes::model()->findByAttributes(array('class_titile' => trim($classcount[$i])));
                            if (isset($classValue->id) && $classValue->id <> "") {
                                $ClassCourseCategoryUsersRestrictionCoupon->setAttribute('classes_id', $classValue->id);
                            }
                        }if (isset($coursecount[$i])) {
                            if ($coursecount[$i] == "All") {
                                $ClassCourseCategoryUsersRestrictionCoupon->setAttribute('courses_id', 0);
                            }
                            $courseValue = Courses::model()->findByAttributes(array('course_titile' => trim($coursecount[$i])));
                            if (isset($courseValue->id) && $courseValue->id <> "") {
                                $ClassCourseCategoryUsersRestrictionCoupon->setAttribute('courses_id', $courseValue->id);
                            }
                        }

                        $ClassCourseCategoryUsersRestrictionCoupon->save();
                    }
                }
                $this->redirect(array('admin'));
            }
        }

        for ($i = 0; $i < count($model->classCourseCategoryUsersRestrictionCoupons); $i++) {
            if ($model->classCourseCategoryUsersRestrictionCoupons[$i]->classes_id <> "" && $model->classCourseCategoryUsersRestrictionCoupons[$i]->classes_id == 0) {
                $updateclass.='All,';
            }
            if (isset($model->classCourseCategoryUsersRestrictionCoupons[$i]->classes->class_titile)) {
                $updateclass.=$model->classCourseCategoryUsersRestrictionCoupons[$i]->classes->class_titile . ', ';
            }
        }
        for ($i = 0; $i < count($model->classCourseCategoryUsersRestrictionCoupons); $i++) {
            if ($model->classCourseCategoryUsersRestrictionCoupons[$i]->courses_id <> "" && $model->classCourseCategoryUsersRestrictionCoupons[$i]->courses_id == 0) {
                $updatecourse.='All,';
            }

            if (isset($model->classCourseCategoryUsersRestrictionCoupons[$i]->courses->course_titile)) {
                $updatecourse.=$model->classCourseCategoryUsersRestrictionCoupons[$i]->courses->course_titile . ', ';
            }
        }
        $this->render('update', array(
            'model' => $model,
            'ClassCourseCategoryUsersRestrictionCoupon' => $ClassCourseCategoryUsersRestrictionCoupon, 'updateclass' => $updateclass, 'updatecourse' => $updatecourse,
        ));
    }

    public function actionStatuschange() {
        $model = $this->loadModel($_POST['id'], 'Coupons');
        $model->status = $_POST['status'];
        $model->save(false);
    }

    public function actionDelete($id) {
        if (Yii::app()->getRequest()->getIsPostRequest()) {
            $model = $this->loadModel($id, 'Coupons');
            Coupons::deleteClass_course_category_users_restriction_coupon($model->id);
            $this->loadModel($id, 'Coupons')->delete();

            if (!Yii::app()->getRequest()->getIsAjaxRequest())
                $this->redirect(array('admin'));
        } else
            throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
    }

    public function actionIndex() {
        $dataProvider = new CActiveDataProvider('Coupons');
        $this->render('index', array(
            'dataProvider' => $dataProvider,
        ));
    }

    public function actionAdmin() {
        $model = new Coupons('search');
        $criteria = new CDbCriteria;
        $criteria->addCondition(array('condition' => 'promotion_coupon=0'));
        $DataProvider = new CActiveDataProvider($model, array('criteria' => $criteria));
        $model->unsetAttributes();
        $statuschangeurl = Yii::app()->createUrl('coupons/statuschange/');
        if (isset($_GET['Coupons']))
            $model->setAttributes($_GET['Coupons']);
        $criteria->compare('status', $model->status);

        $this->render('admin', array(
            'DataProvider' => $DataProvider,
            'model' => $model,
            'statuschangeurl' => $statuschangeurl,
        ));
    }

    public function actionpromocoupon() {
        $model = new Coupons('search');
        $criteria = new CDbCriteria;
        $criteria->addCondition(array('condition' => 'promotion_coupon=1'));
        $DataProvider = new CActiveDataProvider($model, array('criteria' => $criteria));
        $model->unsetAttributes();
        $statuschangeurl = Yii::app()->createUrl('coupons/statuschange/');
        if (isset($_GET['Coupons']))
            $model->setAttributes($_GET['Coupons']);
        $criteria->compare('status', $model->status);
        $criteria->addCondition(array('condition' => 'promotion_coupon=1'));
        $this->render('promocoupon', array(
            'DataProvider' => $DataProvider,
            'model' => $model,
            'statuschangeurl' => $statuschangeurl,
        ));
    }

    public function actionAutocompleteClass() {
        $resall = array('All');
        $res = array();
        if (isset($_GET['term'])) {
            $qtxt = "SELECT class_titile FROM {{classes}} WHERE class_titile LIKE :class_titile AND status=" . SkillConfig::STATUS_ACTIVE . ' AND class_type_id=' . CURATED_CLASS_TYPE;
            $command = Yii::app()->db->createCommand($qtxt);
            $command->bindValue(":class_titile", '%' . $_GET['term'] . '%', PDO::PARAM_STR);
            $res = $command->queryColumn();
        }
        $mergeres = array_merge($resall, $res);
        echo CJSON::encode($mergeres);
        Yii::app()->end();
    }

    public function actionAutocompleteCourse() {
        $resall = array('All');
        $res = array();
        if (isset($_GET['term'])) {
            $qtxt = "SELECT course_titile FROM {{courses}} WHERE course_titile LIKE :course_titile AND status=" . SkillConfig::STATUS_ACTIVE;
            $command = Yii::app()->db->createCommand($qtxt);
            $command->bindValue(":course_titile", '%' . $_GET['term'] . '%', PDO::PARAM_STR);
            $res = $command->queryColumn();
        }
        $mergeres = array_merge($resall, $res);
        echo CJSON::encode($mergeres);
        Yii::app()->end();
    }

}